﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;
using System.Security.Cryptography;

namespace MarginTrading.Forms
{
    public partial class LoginForm : Form
    {
        public bool b_Canceled = false;
        private bool b_Drag = false;
        private Point start_point = new Point(0, 0);

        public LoginForm()
        {
            b_Canceled = false;
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            b_Canceled = true;
            Close();
        }

        private void LoginForm_MouseDown(object sender, MouseEventArgs e)
        {
            this.b_Drag = true;
            this.start_point = new Point(e.X, e.Y);
        }

        private void LoginForm_MouseUp(object sender, MouseEventArgs e)
        {
            this.b_Drag = false;
        }

        private void LoginForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.b_Drag)
            {
                Point p1 = new Point(e.X, e.Y);
                Point p2 = this.PointToScreen(p1);
                Point p3 = new Point(p2.X - this.start_point.X, p2.Y - this.start_point.Y);
                this.Location = p3;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            b_Canceled = true;
            Close();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if  (textBoxUsername.Text == "" || textBoxPassword.Text == "")
            {
                labelLoginFailMsg.Text = "Enter Username and Password";
                //MessageBox.Show("Please Enter Username and Password", "Enter Login Details", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            UserAccount User;
            try
            {
                User = UserAccountServices.GetUserAccountByUserName(textBoxUsername.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Could not connect to the database." + Environment.NewLine + "Check your database connection settings", "Error.");
                DBConnectionSettingsDlg DBConDlg = new DBConnectionSettingsDlg();

                DBConDlg.ShowDialog();

                return;
            }
            
            if (User == null)
            {

                labelLoginFailMsg.Text = "Invalid Username or Password";
                //MessageBox.Show("Invalid Username or Password", "Invalid Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                String sHashedPass = MD5Calculator.CalculateMD5Hash(textBoxPassword.Text);

                if (sHashedPass == User.Password)
                {
                    Program.currentUser = User;
                    Close();
                }
                else
                {
                    labelLoginFailMsg.Text = "Invalid Username or Password";
                    //MessageBox.Show("Invalid Username or Password", "Invalid Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            try
            {
                Config config = ConfigServices.GetConfigByName(Constants.CONFIG_NAME_COMPANY_NAME);
                labelCompanyName.Text = config.Value;
            }
            catch (Exception ex)
            {
                labelCompanyName.Text = "Company Name";
            }

            textBoxUsername.Text = "";
            textBoxPassword.Text = "";
            //System.Windows.Input.Keyboard.Focus((System.Windows.IInputElement)textBoxUsername);
        }

        private void LoginForm_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void textBoxUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                buttonLogin.Focus();
                SendKeys.Send("{ENTER}");
            }
        }

        private void textBoxPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                buttonLogin.Focus();
                SendKeys.Send("{ENTER}");
            }
        }

        private void labelCompanyName_MouseDown(object sender, MouseEventArgs e)
        {
            LoginForm_MouseDown(null, e);
        }

        private void labelCompanyName_MouseMove(object sender, MouseEventArgs e)
        {
            LoginForm_MouseMove(null, e);
        }

        private void labelCompanyName_MouseUp(object sender, MouseEventArgs e)
        {
            LoginForm_MouseUp(null, e);
        }
    }
}
